package com.devin.dao;

import com.devin.pojo.Member;
import com.devin.pojo.Orders;
import com.devin.pojo.Product;
import com.devin.pojo.Traveller;
import com.devin.typeHandler.MyDateTypeHandler;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface OrderMapper {

//    @Select(" select * from orders")
//    @Results({@Result(property="id",column = "id",javaType = int.class,jdbcType = JdbcType.INTEGER),
//            @Result(property = "orderNum",column = "orderNum",javaType = String.class,jdbcType = JdbcType.VARCHAR),
//            @Result(property = "orderTime",column = "orderTime",javaType = String.class,jdbcType = JdbcType.TIMESTAMP,typeHandler = MyDateTypeHandler.class),
//            @Result(property = "peopleCount",column="peopleCount",javaType = int.class,jdbcType = JdbcType.INTEGER),
//            @Result(property = "orderDesc",column="orderDesc",javaType = String.class,jdbcType = JdbcType.VARCHAR),
//            @Result(property = "payType",column="payType",javaType = int.class,jdbcType = JdbcType.INTEGER),
//            @Result(property = "orderStatus",column="orderStatus",javaType = int.class,jdbcType = JdbcType.INTEGER),
//            @Result(property = "productId",column="productId",javaType = int.class,jdbcType = JdbcType.INTEGER),
//            @Result(property = "memberId",column="memberId",javaType = int.class,jdbcType = JdbcType.INTEGER),
//            @Result(property = "product",column="productId",
//                    javaType = Product.class,
//                    one = @One(select = "com.devin.dao.ProductMapper.findProductById"))
//    })
//    List<Orders> findOrders();

        @Select(" select * from orders")
        @Results({@Result(id = true,property="id",column = "id"/*,javaType = int.class,jdbcType = JdbcType.INTEGER*/),
                @Result(property = "orderNum",column = "orderNum"/*,javaType = String.class*/,jdbcType = JdbcType.VARCHAR),
                @Result(property = "orderTime",column = "orderTime",javaType = String.class,jdbcType = JdbcType.TIMESTAMP,typeHandler = MyDateTypeHandler.class),
                @Result(property = "peopleCount",column="peopleCount"/*,javaType = int.class*/,jdbcType = JdbcType.INTEGER),
                @Result(property = "orderDesc",column="orderDesc"/*,javaType = String.class*/,jdbcType = JdbcType.VARCHAR),
                @Result(property = "payType",column="payType"/*,javaType = int.class*/,jdbcType = JdbcType.INTEGER),
                @Result(property = "orderStatus",column="orderStatus"/*,javaType = int.class*/,jdbcType = JdbcType.INTEGER),
                @Result(property = "productId",column="productId"/*,javaType = int.class*/,jdbcType = JdbcType.INTEGER),
                @Result(property = "memberId",column="memberId"/*,javaType = int.class*/,jdbcType = JdbcType.INTEGER),
                @Result(property = "product",column="productId",
                        javaType = Product.class,
                        one = @One(select = "com.devin.dao.ProductMapper.findProductById"))
    })
    List<Orders> findOrders();

    /*添加订单order*/
    @Insert(" insert into orders values(null,replace(uuid(),'-',''),null,#{peopleCount,javaType=Integer,jdbcType=INTEGER},#{orderDesc,javaType=String,jdbcType=VARCHAR},#{payType,javaType=Integer,jdbcType=INTEGER},#{orderStatus,javaType=Integer,jdbcType=INTEGER},#{productId,javaType=Integer,jdbcType=INTEGER},#{memberId,javaType=Integer,jdbcType=INTEGER})")
    @Options(keyProperty = "id",keyColumn = "id", useGeneratedKeys = true)
    public void addOrder(Orders order);

    /*添加游客traveller*/
    @Insert(" insert into traveller values(null,#{name,jdbcType=VARCHAR},#{sex,jdbcType=VARCHAR},#{phoneNum,jdbcType=VARCHAR},#{credentialsType,jdbcType=INTEGER},#{credentialsNum,jdbcType=VARCHAR},#{travellerType,jdbcType=INTEGER}) ")
    @Options(keyProperty = "id",keyColumn = "id", useGeneratedKeys = true)
    void addTravellers(Traveller traveller);

    /*添加中间表，游客-订单*/
    @Insert("insert into order_traveller values(#{orderId,jdbcType=INTEGER},#{travellerId,jdbcType=INTEGER})")
    void addOrderAndTraveller(@Param("orderId") Integer orderId,@Param("travellerId") Integer travellerId);

    /*通过订单ID查询订单所有信息*/
    @Select("select * from orders where id=#{orderId}")
    @Results({
            @Result(id = true,property = "id",column = "id",jdbcType = JdbcType.INTEGER),
            @Result(property = "orderNum",column = "orderNum",jdbcType = JdbcType.VARCHAR),
            @Result(property = "orderTime",column = "orderTime",typeHandler = MyDateTypeHandler.class),
            @Result(property = "peopleCount",column = "peopleCount",jdbcType = JdbcType.INTEGER),
            @Result(property = "orderDesc",column = "orderDesc",jdbcType = JdbcType.VARCHAR),
            @Result(property = "payType",column = "payType",jdbcType = JdbcType.INTEGER),
            @Result(property = "orderStatus",column = "orderStatus",jdbcType = JdbcType.INTEGER),
            @Result(property = "productId ",column = "productId ",jdbcType = JdbcType.INTEGER),
            @Result(property = "memberId",column = "memberId",jdbcType = JdbcType.INTEGER),
            @Result(property = "product",column = "productId",
                    javaType = Product.class,
                    one = @One(select ="com.devin.dao.ProductMapper.findProductById")),
            @Result(property = "member",column = "memberId",
                    javaType = Member.class,
                    one = @One(select = "com.devin.dao.MemberMapper.getMemberById")),
            @Result(property = "travellerList",column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.devin.dao.TravellerMapper.getTravellerById"))
    })
    Orders getOrdersInfoById(int orderId);

    @Update(" delete from orders where id=#{orderId} ")
    void delOrder(int orderId);
}


